<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>tree菜单</title>
    <style>
        [v-cloak] {
            display: none
        }
    </style>
</head>
<body>

<div id="app">
    <h1>{{ title }}</h1>
    <sali-tree :data="dataList"></sali-tree>
</div>

<script src="/js/vue.min.js"></script>
<script src="/js/axios.min.js"></script>
<script>

    Vue.component("sali-tree", {
        props: {
            data: Array
        },
        template: "<ul>" +
            "<li v-for='(d,index) in data' @click.stop='expand(d)'>" +
            "<span>" +
            "<span v-if='d.type==1'>目录</span>" +
            "<span v-if='d.type==2'>文件</span>  " +
            "{{d.title}}</span>" +
            "<sali-tree v-show='d.isexpand' v-if='d.childrenList' :data='d.childrenList'></sali-tree>" +
            "</li>" +
            "</ul>",
        methods: {
            expand: function (obj) {
                obj.isexpand = !obj.isexpand;
            }
        }
    })

    var vue = new Vue({
        el: "#app",
        data: {
            title: "Hello, Salieri",
            dataList: []
        },
        created: function () {
            // 页面加载初始化获取数据
            this.loadTree();
        },
        methods: {
            // 加载tree的数据信息
            loadTree: function () {
                axios.get("/api/category/tree").then(res => {
                    this.dataList = res.data
                })
            }
        }
    })
</script>

</body>
</html>